package com.portal.business.price;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.portal.business.functions.dao.FunctionsDao;
import com.portal.business.functions.dto.FunctionsDTO;
import com.portal.business.price.dao.PriceDao;
import com.portal.business.price.dto.PriceDTO;
import com.portal.business.price.dto.PriceResultBean;
import com.portal.framework.common.PortalSearchBean;
import com.portal.framework.page.Pager;


	
/**
 * 
 * 项目名称: Portal     
 * 类名称:   PriceServiceImpl     
 * 类描述:   价格标准 
 * 创建人:   骆海东
 * 创建时间: Aug 31, 2011 3:13:05 PM     
 * 最后修改人:   骆海东
 * 最后修改时间: Aug 31, 2011 3:13:05 PM     
 * 最后修改备注:
 */
public class PriceServiceImpl implements PriceService {

	private PriceDao priceDao;
	
	private FunctionsDao functionsDao;
	
	public Boolean addPrice(PriceDTO priceDTO) {
		if(this.priceDao.create(priceDTO)!=null)return true;
		else return false;
	}

	public int delPrice(Integer priceId) {
		return this.priceDao.delete(priceId);
	}

	public int delPriceBatch(String[] asIdsStr) {
		int len=asIdsStr.length;
		Integer[] ids=new Integer[len];
		for (int i = 0; i <len; i++) {
			ids[i]=Integer.valueOf(asIdsStr[i]);
		}
		return this.priceDao.deleteBatch(ids);
	}

	public PriceDTO findByID(Integer priceId) {
		PriceDTO p= this.priceDao.findById(priceId);
		Set<FunctionsDTO> fs=p.getFunSet();
		List<Integer> ids=new ArrayList<Integer>();
		if(fs.size()>0) {
			Iterator<FunctionsDTO> it=fs.iterator();
			while(it.hasNext()){
			FunctionsDTO f=it.next();
			ids.add(f.getFunId());
			}
		}
		p.setFunSet(null);//系统后台调用不了门户项目的延迟加载,要将该数据设为空
		p.setFunIds(ids);//系统后台依靠该id集，判断资费对应的功能模块
		return p;
	}

	public PriceResultBean findPrice(PortalSearchBean portalSearchBean) {

		String selecthql="select new PriceDTO(priceId,name,money,num,pdesc,createDate)";//查数据hql
		String countHql="select count(priceId)";//查总数hql
		String hql=" from PriceDTO ";//条件hql
		//用于保存查找输入参数集合
		List<Object> osList=new ArrayList<Object>();
		if(portalSearchBean.getName()!=null&&!portalSearchBean.getName().trim().equals("")){
			hql=hql+"where name like ?";
			osList.add("%"+portalSearchBean.getName()+"%");
		}
		hql=hql+" order by priceId desc";
		//获取总数
		int count=this.priceDao.countQuery(countHql+hql,osList.toArray());
		//设置页码对象
		Pager pager=new Pager();
		pager.setPageSize(portalSearchBean.getPageSize());
		pager.setTotal(count);
		pager.setCurrentPage(portalSearchBean.getPage());
		//获取数据
		List<PriceDTO> olsit=this.priceDao.find(pager.getStart(),pager.getPageSize(), PriceDTO.class,selecthql+hql, osList.toArray());
		//设置返回对象
		PriceResultBean pb=new PriceResultBean();
		pb.setCount(count);
		pb.setResultList(olsit);
		return pb;
	}

	public Boolean updatePrice(PriceDTO priceDTO) {
		PriceDTO p=this.priceDao.findById(priceDTO.getPriceId());
		p.setMoney(priceDTO.getMoney());
		p.setName(priceDTO.getName());
		p.setNum(priceDTO.getNum());
		p.setPdesc(priceDTO.getPdesc());
		if(this.priceDao.update(p)!=null)return true;
		else return false;
	}

	
	public boolean updatePriceFun(Integer priceId, List<Integer> fids) {
		PriceDTO p=this.priceDao.findById(priceId);
		if(fids!=null){
			StringBuffer sb=new StringBuffer();
			sb.append("0");
			int len=fids.size();
			for (int i = 0; i < len; i++) {
				sb.append(",");
				sb.append(fids.get(i));
			}
			String hql="from FunctionsDTO where funId in("+sb.toString()+")";
			List<FunctionsDTO> fs=this.functionsDao.find(hql);
			Set<FunctionsDTO> fSet = new HashSet<FunctionsDTO>();
	        for (int i = 0; i < fs.size(); i++) {
	        	fSet.add(fs.get(i));
			}
			p.setFunSet(fSet);
		}else{
			p.setFunSet(null);
		}
		PriceDTO pnew=this.priceDao.update(p);
		
		if(pnew!=null)return true;
		else return false;
	}
	
	public PriceDao getPriceDao() {
		return priceDao;
	}

	public void setPriceDao(PriceDao priceDao) {
		this.priceDao = priceDao;
	}

	public FunctionsDao getFunctionsDao() {
		return functionsDao;
	}

	public void setFunctionsDao(FunctionsDao functionsDao) {
		this.functionsDao = functionsDao;
	}

}

		